package me.sealer.ssm.service.impl;

import me.sealer.ssm.constant.PageConstant;
import me.sealer.ssm.mapper.ChinaAreaMapper;
import me.sealer.ssm.model.ChinaArea;
import me.sealer.ssm.model.ChinaAreaExample;
import me.sealer.ssm.page.PageResult;
import me.sealer.ssm.service.ChinaAreaService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 功能描述： $ ChinaAreaServiceImpl
 *
 * @author sealer
 * @email 1178884049@qq.com
 * @date 2019年11月15日 13时50分18秒
 */
@Service
public class ChinaAreaServiceImpl implements ChinaAreaService {
    private final ChinaAreaMapper chinaAreaMapper;

    public ChinaAreaServiceImpl(ChinaAreaMapper chinaAreaMapper) {
        this.chinaAreaMapper = chinaAreaMapper;
    }

    @Override
    public List<ChinaArea> queryAreaList(String name) {
        ChinaAreaExample example = new ChinaAreaExample();
        example.createCriteria()
                .andNameLike("%" + name + "%");

        return chinaAreaMapper.selectByExample(example);
    }

    @Override
    public PageResult<ChinaArea> queryAreaList(String name, Integer level, Integer zipCode, String cityCode, Long code, Long parentCode, Integer pageNo, Integer pageSize) {
        if (pageSize == null) {
            pageSize = PageConstant.DEFAULT_PAGE_SIZE;
        }

        if (pageSize > PageConstant.MAX_PAGE_SIZE) {
            pageSize = PageConstant.MAX_PAGE_SIZE;
        }

        if (pageNo == null) {
            pageNo = PageConstant.DEFAULT_PAGE_NO;
        }

        ChinaAreaExample example = genExample(name, level, zipCode, cityCode, code, parentCode);
        example.setLimit(pageSize);
        example.setStart((pageNo - 1) * pageSize);
        List<ChinaArea> chinaAreaList = chinaAreaMapper.selectByExample(example);

        if (chinaAreaList == null) {
            chinaAreaList = new ArrayList<>();
        }

        int count = countAreaList(name, level, zipCode, cityCode, code, parentCode);

        PageResult<ChinaArea> pageResult = new PageResult<>();
        pageResult.setList(chinaAreaList);
        pageResult.setPageNo(pageNo == null ? PageConstant.DEFAULT_PAGE_NO : pageNo);

        pageResult.setTotalPages((count + pageSize - 1) / pageSize);
        pageResult.setTotalElements(count);
        pageResult.setFirst(pageNo == PageConstant.FIRST_PAGE_NO);
        pageResult.setLast(pageNo == (count + pageSize - 1) / pageSize);
        pageResult.setPageSize(pageSize);

        return pageResult;
    }

    @Override
    public Integer countAreaList(String name, Integer level, Integer zipCode, String cityCode, Long code, Long parentCode) {
        ChinaAreaExample example = genExample(name, level, zipCode, cityCode, code, parentCode);
        example.setLimit(null);
        example.setStart(null);
        return chinaAreaMapper.countByExample(example);

    }

    @Override
    public List<ChinaArea> queryAreas(int level) {
        ChinaAreaExample example = new ChinaAreaExample();
        example.createCriteria()
                .andLevelEqualTo(level);

        return chinaAreaMapper.selectByExample(example);
    }

    @Override
    public List<ChinaArea> queryAreas(int level, long code) {
        ChinaAreaExample example = new ChinaAreaExample();
        example.setOrderByClause(" pinyin asc");
        example.createCriteria()
                .andLevelEqualTo(level)
                .andParentCodeEqualTo(code);

        return chinaAreaMapper.selectByExample(example);
    }

    private ChinaAreaExample genExample(String name, Integer level, Integer zipCode, String cityCode, Long code, Long parentCode) {
        ChinaAreaExample example = new ChinaAreaExample();

        ChinaAreaExample.Criteria criteria = example.createCriteria();

        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        if (Objects.nonNull(level)) {
            criteria.andLevelEqualTo(level);
        }

        if (Objects.nonNull(zipCode)) {
            criteria.andZipCodeEqualTo(zipCode);
        }

        if (cityCode != null && !"".equals(cityCode)) {
            criteria.andCityCodeEqualTo(cityCode);
        }

        if (Objects.nonNull(code)) {
            criteria.andCodeEqualTo(code);
        }

        if (Objects.nonNull(parentCode)) {
            criteria.andParentCodeEqualTo(parentCode);
        }

        return example;
    }
}
